package com.mycompany.crm.controller;

import com.mycompany.crm.annotation.LogAnno;
import com.mycompany.crm.annotation.RequiredPermission;
import com.mycompany.crm.base.BaseController;
import com.mycompany.crm.base.ResultInfo;
import com.mycompany.crm.query.DataDicQuery;
import com.mycompany.crm.service.DataDicService;
import com.mycompany.crm.vo.DataDic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author jy
 * @version 1.0
 * @date 2022/4/3 14:45
 */
@Controller
@RequestMapping("data_dic")
public class DataDicController extends BaseController {

    @Autowired
    private DataDicService dataDicService;

    /**
     * 进入数据字典页面
     * @return
     */
    @LogAnno(description = "进入数据字典页面")
    @RequiredPermission(code = "6040")
    @RequestMapping("index")
    public String index(){
        return "datadic/data_dic";
    }


    /**
     * 字典管理数据查询(分页多条件查询)
     * @param dataDicQuery
     * @return
     */
    @LogAnno(description = "字典管理数据查询(分页多条件查询)")
    @RequiredPermission(code = "604002")
    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> selectByParams(DataDicQuery dataDicQuery){
        return dataDicService.queryByParamsForTable(dataDicQuery);
    }

    /**
     * 进入添加/更新数据字典的界面
     * @return
     */
    @LogAnno(description = "进入添加/更新数据字典的界面")
    @RequestMapping("toAddOrUpdateDataDicPage")
    public String toAddOrUpdateDataDicPage(Integer id, HttpServletRequest request){
        //判断userId是否为空
        if (id != null){
            //通过Id查询用户数据便于回显
            DataDic dataDic = dataDicService.selectByPrimaryKey(id);
            //将数据设置到请求域中
            request.setAttribute("dataDicInfo",dataDic);
        }
        return "datadic/add_update";
    }

    /**
     * 添加数据字典
     * @param dataDic
     * @return
     */
    @LogAnno(description = "添加数据字典")
    @RequiredPermission(code = "604001")
    @PostMapping("save")
    @ResponseBody
    public ResultInfo saveDataDic(DataDic dataDic){
        dataDicService.saveDataDic(dataDic);
        return success("数据字典添加成功!");
    }

    /**
     * 更新数据字典
     * @param dataDic 传入的待更新的用户对象
     * @return
     */
    @LogAnno(description = "更新数据字典")
    @RequiredPermission(code = "604003")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateDataDic(DataDic dataDic){
        dataDicService.updateDataDic(dataDic);
        return success("数据字典更新成功!");
    }

}
